/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui;

import com.logical.allhit.gui.action.StartAction;
import com.logical.allhit.gui.model.AccountTableModel;
import com.logical.allhit.util.ScrollUtil;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JTable;
/**
 *
 * @author Sanjeet Jha
 */
public class MainFrom extends javax.swing.JPanel implements PropertyChangeListener{

    /**
     * Creates new form MainFrom
     */
    public MainFrom() {
        initComponents();
        accountTable.setModel(new AccountTableModel());
        accountTable.getColumnModel().getColumn(0).setPreferredWidth(20);
        accountTable.getColumnModel().getColumn(0).setMinWidth(20);
        accountTable.getColumnModel().getColumn(0).setMaxWidth(20);
        accountTable.getColumnModel().getColumn(2).setPreferredWidth(50);
//        accountTable.getColumnModel().getColumn(3).setPreferredWidth(30);
        accountTable.getColumnModel().getColumn(3).setPreferredWidth(100);
//        accountTable.getColumnModel().getColumn(4).setPreferredWidth(30);
        accountTable.getColumnModel().getColumn(4).setPreferredWidth(50);
//        accountTable.getColumnModel().getColumn(5).setPreferredWidth(30);
        accountTable.getColumnModel().getColumn(2).setMinWidth(50);
//        accountTable.getColumnModel().getColumn(3).setMinWidth(30);
        accountTable.getColumnModel().getColumn(3).setMinWidth(100);
//        accountTable.getColumnModel().getColumn(4).setMinWidth(30);
        accountTable.getColumnModel().getColumn(4).setMinWidth(50);
//        accountTable.getColumnModel().getColumn(5).setMinWidth(30);
        accountTable.getColumnModel().getColumn(2).setMaxWidth(70);
//        accountTable.getColumnModel().getColumn(3).setMaxWidth(50);
        accountTable.getColumnModel().getColumn(3).setMaxWidth(140);
//        accountTable.getColumnModel().getColumn(4).setMaxWidth(50);
        accountTable.getColumnModel().getColumn(4).setMaxWidth(70);
//        accountTable.getColumnModel().getColumn(5).setMaxWidth(50);
        accountTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        accountTable.getColumnModel().getColumn(1).setMaxWidth(140);
        accountTable.getColumnModel().getColumn(1).setMinWidth(100);
//        accountTable.getColumnModel().getColumn(6).setPreferredWidth(100);
//        accountTable.getColumnModel().getColumn(6).setMaxWidth(140);
//        accountTable.getColumnModel().getColumn(6).setMinWidth(100);
//        accountTable.getColumnModel().getColumn(7).setPreferredWidth(50);
//        accountTable.getColumnModel().getColumn(7).setMaxWidth(70);
//        accountTable.getColumnModel().getColumn(7).setMinWidth(50);
        accountTable.setAutoCreateRowSorter(true);
        accountTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }

    public AccountTableModel getAccountTableModel(){
        return (AccountTableModel) accountTable.getModel();
    }
    
    public  boolean[] getSelection(){
        boolean[] selecttion = new boolean[3];
        selecttion[0] = addCheckBox.isSelected();
        selecttion[1] = balanceCheckBox.isSelected();
        return selecttion;
    }
    
    public int getDay(){
        return  Integer.parseInt(dayTextField.getText());
    }
    
    public int getToDay(){
        return  Integer.parseInt(toDayTextField.getText());
    }
    
    public boolean isFormLast(){
        return formLastCheckBox.isSelected();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progressBar = new javax.swing.JProgressBar();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        msgTextPane = new javax.swing.JTextPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        accountTable = new javax.swing.JTable();
        startButton = new javax.swing.JButton();
        addCheckBox = new javax.swing.JCheckBox();
        balanceCheckBox = new javax.swing.JCheckBox();
        lblimage = new javax.swing.JLabel();
        dayTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        toDayTextField = new javax.swing.JTextField();
        formLastCheckBox = new javax.swing.JCheckBox();

        jSplitPane1.setDividerLocation(650);

        jScrollPane2.setViewportView(msgTextPane);

        jSplitPane1.setRightComponent(jScrollPane2);

        accountTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(accountTable);

        startButton.setAction(new StartAction(this));
        startButton.setText("Start");

        addCheckBox.setSelected(true);
        addCheckBox.setText("Cilck Add");

        balanceCheckBox.setSelected(true);
        balanceCheckBox.setText("Balance");

        lblimage.setText(".");

        dayTextField.setColumns(2);
        dayTextField.setText("0");

        jLabel1.setText("To");

        toDayTextField.setColumns(2);
        toDayTextField.setText("1");

        formLastCheckBox.setText("form Last click");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(addCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(balanceCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(toDayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(formLastCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblimage)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startButton)
                    .addComponent(addCheckBox)
                    .addComponent(balanceCheckBox)
                    .addComponent(lblimage)
                    .addComponent(dayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(toDayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(formLastCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSplitPane1.setLeftComponent(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSplitPane1)
                    .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 978, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable accountTable;
    private javax.swing.JCheckBox addCheckBox;
    private javax.swing.JCheckBox balanceCheckBox;
    private javax.swing.JTextField dayTextField;
    private javax.swing.JCheckBox formLastCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lblimage;
    private javax.swing.JTextPane msgTextPane;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JButton startButton;
    private javax.swing.JTextField toDayTextField;
    // End of variables declaration//GEN-END:variables

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            int progress = (Integer) evt.getNewValue();
            progressBar.setValue(progress);
        } 
    }
    
    public void showMsg(String msg){
        if(msgTextPane.getText().length() > 5000){
            msgTextPane.setText(msgTextPane.getText().substring(500));
        }
        msgTextPane.setText( msgTextPane.getText() + "\n" +msg );
        ScrollUtil.scroll(msgTextPane, ScrollUtil.LEFT, ScrollUtil.BOTTOM);
    }

    public void clearLog() {
        msgTextPane.setText("" );
        ScrollUtil.scroll(msgTextPane, ScrollUtil.LEFT, ScrollUtil.BOTTOM);
    }
}
